/*
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef IMAGE_KNIFE_C_KEKE_IMAGE_DATA_CACHE_INTERNAL_H
#define IMAGE_KNIFE_C_KEKE_IMAGE_DATA_CACHE_INTERNAL_H

#include <memory>
#include <multimedia/image_framework/image/pixelmap_native.h>
#include <arkui/native_type.h>
#include "imageknife_type.h"
#include "image_data_cache.h"
#include "image_data.h"

namespace ImageKnifeC {
struct ImageDataCacheInternal : ImageDataCache {
public:
    // 原始图片宽度
    uint32_t imageWidth = 0;
    // 原始图片高度
    uint32_t imageHeight = 0;
    // 缓存所消耗的字节数，比实际的有所误差
    uint32_t cacheSize = 0;
    // 图片编码类型：jpg,png,gif,webp,etc
    ImageFormat type = ImageFormat::UNKNOWN;

    uint32_t GetImageWidth() const override
    {
        return imageWidth;
    }

    uint32_t GetImageHeight() const override
    {
        return imageHeight;
    }

    uint32_t GetImageCacheSize() const override
    {
        return cacheSize;
    }

    ImageFormat GetImageFormat() const override
    {
        return type;
    }

    explicit ImageDataCacheInternal(const char *base64) : base64_(base64)
    {
    }

    explicit ImageDataCacheInternal(OH_PixelmapNative *pixelmap)
        : imageDesc_(OH_ArkUI_DrawableDescriptor_CreateFromPixelMap(pixelmap))
    {
    }

    ImageDataCacheInternal(std::shared_ptr<ImageData> data, bool isAnimator)
    {
        if (isAnimator) {
            frameCount_ = data->GetFrameCount();
            OH_PixelmapNative **pixelmapList = data->GetPixelmapList();
            int *delayList = data->GetDelayTimeList();

            frameInfoList_ = new ArkUI_ImageAnimatorFrameInfo *[frameCount_];
            for (int i = 0; i < frameCount_; i++) {
                auto imgDesc = OH_ArkUI_DrawableDescriptor_CreateFromPixelMap(pixelmapList[i]);
                frameInfoList_[i] =  OH_ArkUI_ImageAnimatorFrameInfo_CreateFromDrawableDescriptor(imgDesc);
                if (delayList != nullptr) {
                    OH_ArkUI_ImageAnimatorFrameInfo_SetDuration(frameInfoList_[i], delayList[i]);    
                }
            }
        } else {
            imageDesc_ = OH_ArkUI_DrawableDescriptor_CreateFromPixelMap(data->GetPixelmap());
        }
    }

    const char *GetBase64() const
    {
        return base64_;
    }

    ArkUI_DrawableDescriptor *GetDrawableDescriptor() const
    {
        return imageDesc_;
    }

    ArkUI_ImageAnimatorFrameInfo **GetFrameInfoList() const
    {
        return frameInfoList_;
    }

    uint32_t GetFrameCount() const
    {
        return frameCount_;
    }

    ~ImageDataCacheInternal()
    {
        if (base64_ != nullptr) {
            delete [] base64_;
        }

        if (frameInfoList_ != nullptr) {
            for (int i = 0; i < frameCount_; i++) {
                OH_ArkUI_ImageAnimatorFrameInfo_Dispose(frameInfoList_[i]);
            }
            delete [] frameInfoList_;
        }

        if (imageDesc_ != nullptr) {
            OH_ArkUI_DrawableDescriptor_Dispose(imageDesc_);
        }
    }
private:
    // base64 编码string 用于Image组件简单显示动图
    const char *base64_ = nullptr;
    // 动图 FrameInfo 数组
    ArkUI_ImageAnimatorFrameInfo **frameInfoList_ = nullptr;
    // 动图 FrameInfo 数组长度
    uint32_t frameCount_ = 0;
    // 图片可写描述符
    ArkUI_DrawableDescriptor *imageDesc_ = nullptr;
};

}

#endif //IMAGE_KNIFE_C_KEKE_IMAGE_DATA_CACHE_INTERNAL_H
